using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace DawnOfConquest
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Main : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;


        //Background textures for the various screens in the game
        Texture2D mTitleScreenBackground;

        //Screen State variables to indicate what is the current screen
        bool mIsTitleScreenShown;

        //background music
        Song musicTitle, musicBattle;

        private ArmyManagment manager;
        private TacticalBattle battle;

        private List<Piece> army1;
        private List<Piece> army2;
        private TacMap map;

        private Boolean inBattle;

        public Main()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            graphics.PreferredBackBufferHeight = 940;
            graphics.PreferredBackBufferWidth = 1200;


        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            
            // Start with menu screen
            mIsTitleScreenShown = true;
            // inBattle = true; // Start with instand battle

            this.IsMouseVisible = true;
            //manager = new ArmyManagment();

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            army1 = new List<Piece>();
            army2 = new List<Piece>();

            //Load the screen backgrounds
            mTitleScreenBackground = Content.Load<Texture2D>("Menu/TitleScreen");

            //Music
            musicTitle = Content.Load<Song>("Music/Title");
            

            //Play the Intro Song
            MediaPlayer.Play(musicTitle);
            /*
                army1.Add( new Pikeman("RED", Content));
                army2.Add(new Pikeman("BLUE", Content));

                army1.Add(new Knight("RED", Content));
                army2.Add( new Knight("BLUE", Content));

                army1.Add(new Swordsman("RED", Content));
                army2.Add(new Swordsman("BLUE", Content));

                army1.Add(new Archer("RED", Content));
                army2.Add(new Archer("BLUE", Content));

                army1.Add(new Nomad("RED", Content));
                army2.Add(new Nomad("BLUE", Content));

                army1.Add(new Mage("RED", Content));
                army2.Add(new Mage("BLUE", Content));

                army1.Add(new Zealot("RED", Content));
                army2.Add(new Zealot("BLUE", Content));
            */

            Tile[,] mm = new Tile[30,30];
            for (int i = 0; i < 30; i++)
                for (int j = 0; j < 30; j++)
                    mm[i,j] = new Tile("Plain", i, j);

            map = new TacMap(mm, Content);
            map.randomMap();

            battle = new TacticalBattle(army1, army2, map, Content);
            manager = new ArmyManagment(Content, army1, army2);

            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        /// 
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            if (inBattle)
            {
                battle.update();
                //this.IsMouseVisible = battle.mouseVisible;
                if (battle.battleOver)
                { 
                    inBattle = false;
                    manager.addBlueGold(100);
                    manager.addRedGold(100);
                    manager.returnToManage(Content);
                }
                //show victory screen
            }
            else if (mIsTitleScreenShown)
            {
                UpdateTitleScreen();
                if (manager.startBattle)
                {
                    inBattle = true;
                }
            }
            else
            {
                manager.update(Content);
                if (manager.startBattle)
                {
                    inBattle = true;
                    map.randomMap();
                    battle = new TacticalBattle(army1, army2, map, Content);
                }
            }

            base.Update(gameTime);
        }

        private void UpdateTitleScreen()
        {
            //Press Space to Start Instant Battle)

            if (Keyboard.GetState().IsKeyDown(Keys.Space) == true)
            {
                mIsTitleScreenShown = false;
                inBattle = false;
                return;
            }

        }



        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin();

            if (inBattle)
            {
                battle.draw(spriteBatch, Content);
            }
            else if (mIsTitleScreenShown)
            {

                DrawTitleScreen();
            }
            else
                manager.draw(spriteBatch, Content);

          
            base.Draw(gameTime);

            spriteBatch.End();
        }

        private void DrawTitleScreen()
        {   //Draw all of the elements that are part of the Title screen
            spriteBatch.Draw(mTitleScreenBackground, Vector2.Zero, Color.White);
            
        }


    }
}
